package com.test.testmap

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.io.RequestConfiguration
import com.esri.arcgisruntime.layers.Layer
import com.esri.arcgisruntime.layers.WebTiledLayer
import com.esri.arcgisruntime.layers.WmtsLayer
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.Basemap
import com.esri.arcgisruntime.mapping.view.MapView
import com.test.testmap.layer.AMapTiledLayerClass
import com.test.testmap.layer.TianDiTuMethodsClass

object MapUtil {

    private fun getRequestConfiguration(): RequestConfiguration {
        ArcGISRuntimeEnvironment.setLicense(MapConstants.ARCGIS_LICENSE)
        val requestConfiguration = RequestConfiguration()
        requestConfiguration.headers["referer"] = MapConstants.ARCGIS
//        MapView["referer"] = MapConstants.ARCGIS
        return requestConfiguration
    }

//    fun getMapLayer(): Layer {
//        //切换图层在这里修改
//        return getWmtsLayer()
//    }

    fun loadTianDiTu(): Basemap {
        // 加载天地图层-电子地图
        val vecordBaseTiledLayer: WebTiledLayer =
            TianDiTuMethodsClass.CreateTianDiTuTiledLayer(TianDiTuMethodsClass.LayerType.TIANDITU_VECTOR_MERCATOR)
        //加载中文标注图层-电子地图
        val vecordWordTiledLayer: WebTiledLayer =
            TianDiTuMethodsClass.CreateTianDiTuTiledLayer(TianDiTuMethodsClass.LayerType.TIANDITU_VECTOR_ANNOTATION_CHINESE_MERCATOR)
        vecordBaseTiledLayer.requestConfiguration = getRequestConfiguration()
        vecordWordTiledLayer.requestConfiguration = getRequestConfiguration()
        vecordBaseTiledLayer.loadAsync()
        vecordWordTiledLayer.loadAsync()
        val basemap = Basemap()
        basemap.baseLayers.add(vecordBaseTiledLayer)
        basemap.baseLayers.add(vecordWordTiledLayer)
        return basemap
    }

    fun loadGaoDeDiTu(): Basemap {
        // 加载高德地图
        val webTiledLayer: WebTiledLayer =
            AMapTiledLayerClass.CreateAMapTiledLayer(AMapTiledLayerClass.LayerType.AMAP_VECTOR)
        webTiledLayer.requestConfiguration = getRequestConfiguration()
        webTiledLayer.loadAsync()
        val basemap = Basemap()
        basemap.baseLayers.add(webTiledLayer)
        return basemap
    }


    /***
     * 加载发布到geoserver上面的图层
     * "http://192.0.2.186:8081/geoserver/gwc/service/wmts" 类似这样的地址
     * */
    fun getWmtsLayer(): Basemap {
        val wmtsLayer = WmtsLayer(MapConstants.MAP_URL, MapConstants.LAYER_ID)
        wmtsLayer.requestConfiguration = getRequestConfiguration()
        wmtsLayer.loadAsync()
        val basemap = Basemap()
        basemap.baseLayers.add(wmtsLayer)
        return basemap
    }

    /***
     * 加载自己从水经注下载下来的图层 ，放到tomcat中
     * "http://192.0.2.186:8080/map_xian_dz/{level}/{col}/{row}.png" 类似这样的地址
     * */
    fun getWebTiledLayer(): Basemap {
        val wmtsLayer = WebTiledLayer(MapConstants.MAP_WEB_TILED)
        wmtsLayer.requestConfiguration = getRequestConfiguration()
        wmtsLayer.loadAsync()
        val basemap = Basemap()
        basemap.baseLayers.add(wmtsLayer)
        return basemap
    }
}